package com.cwits.wifi.ui.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;

import com.cwits.wifi.R;
import com.cwits.wifi.ui.activity.MyInfoActivity;
import com.cwits.wifi.util.Dbug;
import com.cwits.wifi.util.PreferencesHelper;
import com.igexin.sdk.PushConsts;
import com.igexin.sdk.PushManager;

public class PushMessageReceiver extends BroadcastReceiver {
    private static final String TAG = "PushMessageReceiver";

    public PushMessageReceiver() {}

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        //Log.d(TAG, "onReceive() action=" + bundle.getInt("action"));

        switch (bundle.getInt(PushConsts.CMD_ACTION)) {
            case PushConsts.GET_CLIENTID:
                // 获取ClientID(CID)
                // 第三方应用需要将CID上传到第三方服务器，并且将当前用户帐号和CID进行关联，以便日后通过用户帐号查找CID进行消息推送
                String cid = bundle.getString("clientid");
               // Log.d(TAG, "PushConsts.GET_CLIENTID cid=" + cid);
                Dbug.i("PushMessageReceiver", "----" + cid + "----");
                break;

            case PushConsts.GET_MSG_DATA:
                // 获取透传数据
                // String appid = bundle.getString("appid");
                byte[] payload = bundle.getByteArray("payload");

                String taskid = bundle.getString("taskid");
                String messageid = bundle.getString("messageid");

                // smartPush第三方回执调用接口，actionid范围为90000-90999，可根据业务场景执行
                boolean result = PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);
                //System.out.println("第三方回执接口调用" + (result ? "成功" : "失败"));
                if (payload != null) {
                    String data = new String(payload);
                    //System.out.println("receiver payload : " + data);
                    Dbug.i("PushMessageReceiver", "----" + taskid + "----" + messageid + "---" + data + "----" + result);
                    //消息通知栏
                    int msmCount = PreferencesHelper.getSharedPreferences(context).getInt("PushMSMCount", 0);
                    ++msmCount;
                    PreferencesHelper.putIntValue(context, "PushMSMCount", msmCount);
                    //发送 接收到推送消息 的广播
                    Intent receiveIntent = new Intent("com.cwits.wifi.receive.pushmsm");
                    context.sendBroadcast(receiveIntent);
                    //Intent infoIntent = new Intent(context, MyInfoActivity.class);
                    //infoIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    //context.startActivity(infoIntent);
                    PreferencesHelper.putBooleanValue(context,"is_getui",true);
                    /*
                    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                    NotificationCompat.Builder mBulider = new NotificationCompat.Builder(context);
                    mBulider.setContentTitle("个推透传消息测试").setContentText(data).setTicker("个推消息来啦")
                            .setWhen(System.currentTimeMillis()).setPriority(Notification.PRIORITY_DEFAULT)
                            .setOngoing(false).setDefaults(Notification.DEFAULT_VIBRATE).setSmallIcon(R.mipmap.push);
                    Intent infoIntent = new Intent(context, MyInfoActivity.class);
                    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, infoIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    mBulider.setContentIntent(pendingIntent);
                    Notification notification = mBulider.build();
                    notification.flags = Notification.FLAG_AUTO_CANCEL;
                    mNotificationManager.notify(0, notification);//第一个参数int id决定消息栏的数量id相同将会被覆盖
                    //记录接收的且未被打开的消息
                    int msmCount = PreferencesHelper.getSharedPreferences(context).getInt("PushMSMCount", 0);
                    ++msmCount;
                    PreferencesHelper.putIntValue(context, "PushMSMCount", msmCount);
                    //发送 接收到推送消息 的广播
                    Intent receiveIntent = new Intent("com.cwits.wifi.receive.pushmsm");
                    context.sendBroadcast(receiveIntent);
                    */
                }else{
                    Dbug.i("PushMessageReceiver", "----" + taskid + "----" + messageid + "---");
                }
                break;

            case PushConsts.THIRDPART_FEEDBACK:
                Dbug.i("PushMessageReceiver", "----THIRDPART_FEEDBACK");
                /*
                 * String appid = bundle.getString("appid"); String taskid =
                 * bundle.getString("taskid"); String actionid = bundle.getString("actionid");
                 * String result = bundle.getString("result"); long timestamp =
                 * bundle.getLong("timestamp");
                 *
                 * Log.d("GetuiSdkDemo", "appid = " + appid); Log.d("GetuiSdkDemo", "taskid = " +
                 * taskid); Log.d("GetuiSdkDemo", "actionid = " + actionid); Log.d("GetuiSdkDemo",
                 * "result = " + result); Log.d("GetuiSdkDemo", "timestamp = " + timestamp);
                 */
                break;

            default:
                break;
        }
    }
}
